new Vue({
    el: "#app",
    data: {
        goodsId: "",
        goods: {},
        imageList: [],
        hotGoodsList: [],
        num: 1,
        totalMoney: 0,
        isCollect: "",
        bigImage: "",
        smallImage: ""
    },
    methods: {
        add: function () {
            this.num++;
            this.totalMoney = this.goods.goodPrice * this.num;
        },
        sub: function () {
            this.num--;
            if (this.num <= 1) {
                this.num = 1;
            }
            this.totalMoney = this.goods.goodPrice * this.num;
        },
        findDetail: function () {
            axios({
                url: `/goods/findDetail/${this.goodsId}`
            }).then(res => {
                if (res.data.flag) {
                    this.goods = res.data.data.goods;
                    this.imageList = res.data.data.imageList;
                    this.isCollect = res.data.data.isCollect;
                    this.totalMoney = this.goods.goodPrice;
                    this.bigImage = this.imageList[0].imageUri;
                    this.smallImage = this.imageList[0].imageUri;
                }
            });
        },
        findHotGoods: function () {
            axios({
                url: "/home/findHotGoods"
            }).then(res => {
                if (res.data.flag) {
                    this.hotGoodsList = res.data.data;
                }
            });
        },
        toDetail: function (goodsId) {
            sessionStorage.setItem("goodsId", goodsId);
            location.href = "goodsDetail.html"
        },
        doCollect: function () {
            axios({
                url: `/goods/doCollect/${this.goods.id}`
            }).then(res => {
                layer.msg(res.data.message);
                if (res.data.flag) {
                    this.isCollect = res.data.data;
                }
            });
        },
        addCar: function () {
            let car = {};
            car.goodsPic = this.goods.goodsPic;
            car.goodsTitle = this.goods.goodTitle;
            car.goodsPrice = this.goods.goodPrice;
            car.goodNum = this.num;
            car.goodsId = this.goods.id;

            axios({
                method: "post",
                url: "/goods/addCar",
                data: car
            }).then(res => {
                layer.msg(res.data.message);
            });
        },
        mouseOver: function (image, index) {
            this.bigImage = image.imageUri;
            this.smallImage = image.imageUri;
            for (let i = 0; i < this.imageList.length ; i++) {
                this.imageList[i].imageStatus = "0";
            }
            this.imageList[index].imageStatus = "1";
        }
    },
    created: function () {
        this.goodsId = sessionStorage.getItem("goodsId");
        this.findDetail();
        this.findHotGoods();
    }
})